package org.bjf.utils;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.instrument.async.LazyTraceExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.Executors;

/**
 * @author bjf
 */
@Component
public class EventBusUtil {

    private static EventBus eventBus;

    @Autowired
    private BeanFactory beanFactory;

    @PostConstruct
    private void init() {
        eventBus = new AsyncEventBus(new LazyTraceExecutor(beanFactory, Executors.newFixedThreadPool(10)));
    }


    public void register(Object obj) {
        eventBus.register(obj);
    }

    public void unregister(Object obj) {
        eventBus.unregister(obj);
    }

    public void post(Object obj) {
        eventBus.post(obj);
    }

}
